package com.thinkfit.jy.fragment;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.ble.BluetoothSimpleAdapter;
import com.thinkfit.jy.ble.BtItemBean;
import com.thinkfit.jy.ble.bluetooth.BluetoothHelper;
import com.thinkfit.jy.ble.bluetooth.interfaces.IBTBoudListener;
import com.thinkfit.jy.ble.bluetooth.interfaces.IBTConnectListener;
import com.thinkfit.jy.ble.bluetooth.interfaces.IBTScanListener;
import com.thinkfit.jy.ble.bluetooth.interfaces.IBTStateListener;
import com.thinkfit.jy.ble.bluetooth.interfaces.IBluetoothHelper;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.ToastUtils;


import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 蓝牙开关与设备列表界面
 */
// 192.168.31.145
public class TNJBluetoothSettingFragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View, BluetoothSimpleAdapter.ItemClickListener {

    private static final String TAG = "BluetoothFragment ------------->";
    private TextView mTvName, mTvNameTip, mTvPairedDeviceTip, mTvUseDeviceTip;
    private RecyclerView mRecyclerPaired, mRecyclerUse;
    private LinearLayout llBluetooth2;
    private BluetoothSimpleAdapter mPairedAdapter, mUseAdapter;
    private IBluetoothHelper mBluetoothHelper;
    private AlertDialog simpleDialog;
    private Switch mSwBluetooth;

    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tnj_fragment_bluetooth_setting;

    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.tvBack);
        mBluetoothHelper = new BluetoothHelper();
        mBluetoothHelper.setBTStateListener(mBTStateListener);//设置打开关闭状态监听
        mBluetoothHelper.setBTScanListener(mBTScanListener);//设置扫描监听
        mBluetoothHelper.setBTBoudListener(mBTBoudListener);//设置配对监听
        mBluetoothHelper.setBTConnectListener(mBTConnectListener);//设置连接监听
        mBluetoothHelper.init(getActivity());
        mRecyclerPaired = (RecyclerView) view.findViewById(R.id.recycler_view_paired);
        mRecyclerUse = (RecyclerView) view.findViewById(R.id.recycler_view_use);
        mSwBluetooth = view.findViewById(R.id.sw_bluetooth);
        mTvName = (TextView) view.findViewById(R.id.tv_name);
        mTvNameTip = (TextView) view.findViewById(R.id.tv_name_tip);
        llBluetooth2 = view.findViewById(R.id.llBluetooth2);
        mTvPairedDeviceTip = (TextView) view.findViewById(R.id.tv_paired_device_tip);
        mTvUseDeviceTip = (TextView) view.findViewById(R.id.tv_use_device_tip);
        mRecyclerPaired.setNestedScrollingEnabled(false);
        mPairedAdapter = new BluetoothSimpleAdapter(getActivity());
        mRecyclerPaired.setLayoutManager(new LinearLayoutManager(getActivity()));
//        mRecyclerPaired.addItemDecoration(new SpacesItemDecoration(10));
        mRecyclerPaired.setAdapter(mPairedAdapter);
        mPairedAdapter.setItemClickListener(this);

        mRecyclerUse.setNestedScrollingEnabled(false);
        mUseAdapter = new BluetoothSimpleAdapter(getActivity());
        mRecyclerUse.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerUse.setAdapter(mUseAdapter);
        mUseAdapter.setItemClickListener(this);
        mSwBluetooth.setChecked(mBluetoothHelper.isEnable());
        if (mBluetoothHelper.isEnable()) {
            mTvNameTip.setVisibility(View.VISIBLE);
//            llBluetooth2.setVisibility(View.VISIBLE);
            mTvName.setVisibility(View.VISIBLE);
            mTvPairedDeviceTip.setVisibility(View.VISIBLE);
            mTvUseDeviceTip.setVisibility(View.VISIBLE);
            mRecyclerPaired.setVisibility(View.VISIBLE);
            mRecyclerUse.setVisibility(View.VISIBLE);
        } else {
            mTvNameTip.setVisibility(View.GONE);
//            llBluetooth2.setVisibility(View.GONE);
            mTvName.setVisibility(View.GONE);
            mTvPairedDeviceTip.setVisibility(View.GONE);
            mTvUseDeviceTip.setVisibility(View.GONE);
            mRecyclerPaired.setVisibility(View.GONE);
            mRecyclerUse.setVisibility(View.GONE);
        }
        mSwBluetooth.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                HashMap<String, Object> map = new HashMap<>();
                if (!buttonView.isPressed())
                    return;
                if (isChecked) {
                    mBluetoothHelper.open();
                    map.put(EventBusEvent.BLUETOOTH_CHANGE, true);
                } else {
                    mBluetoothHelper.close();
                    map.put(EventBusEvent.BLUETOOTH_CHANGE, false);
                }
                EventBus.getDefault().post(map);
            }
        });

        mTvName.setText(mBluetoothHelper.getName());
        getBondedDevices();
        mBluetoothHelper.getConnectedDevices();
    }


    @Override
    public void onResume() {
        super.onResume();
        mBluetoothHelper.startDiscovery();
        SportsStatusBarViewUtils.getInstance().goneLayout();
    }

    @Override
    public void onPause() {
        super.onPause();
        mBluetoothHelper.stopDiscovery();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.tvBack:
                getParentFragmentManager().popBackStack();
                break;
        }
    }

    //蓝牙状态监听
    private IBTStateListener mBTStateListener = new IBTStateListener() {

        /**
         * 蓝牙开关状态
         * int STATE_OFF = 10; //蓝牙关闭
         * int STATE_ON = 12; //蓝牙打开
         * int STATE_TURNING_OFF = 13; //蓝牙正在关闭
         * int STATE_TURNING_ON = 11; //蓝牙正在打开
         */
        @Override
        public void onStateChange(int state) {
            switch (state) {
                case BluetoothAdapter.STATE_OFF:
                    mSwBluetooth.setChecked(mBluetoothHelper.isEnable());
                    mTvNameTip.setVisibility(View.GONE);
                    llBluetooth2.setVisibility(View.GONE);
                    mTvName.setVisibility(View.GONE);
                    mTvPairedDeviceTip.setVisibility(View.GONE);
                    mTvUseDeviceTip.setVisibility(View.GONE);
                    mRecyclerPaired.setVisibility(View.GONE);
                    mRecyclerUse.setVisibility(View.GONE);
                    mPairedAdapter.clear();
                    mPairedAdapter.notifyDataSetChanged();
                    mUseAdapter.clear();
                    mUseAdapter.notifyDataSetChanged();
                    break;
                case BluetoothAdapter.STATE_ON:
                    mSwBluetooth.setChecked(mBluetoothHelper.isEnable());
                    mTvNameTip.setVisibility(View.VISIBLE);
                    llBluetooth2.setVisibility(View.VISIBLE);
                    mTvName.setVisibility(View.VISIBLE);
                    mTvPairedDeviceTip.setVisibility(View.VISIBLE);
                    mTvUseDeviceTip.setVisibility(View.VISIBLE);
                    mRecyclerPaired.setVisibility(View.VISIBLE);
                    mRecyclerUse.setVisibility(View.VISIBLE);
                    getBondedDevices();
                    mBluetoothHelper.setDiscoverableTimeout(300);//设置可见时间
//                Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
//                //讲一个键值对对方到Intent对象当中，用于指定可见状态的持续时间
//                discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
//                startActivity(discoverableIntent);
                    mBluetoothHelper.startDiscovery();
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                case BluetoothAdapter.STATE_TURNING_ON:
                    break;
            }
        }
    };

    //蓝牙搜索监听
    private IBTScanListener mBTScanListener = new IBTScanListener() {
        @Override
        public void onScanStart() {//搜索开始
//            ToastUtils.getInstance().show(getContext(),getString(R.string.blu_serach_device));
        }

        @Override
        public void onScanStop(List<BluetoothDevice> deviceList) {//搜索结束

        }

        /**
         *
         * @param device
         */
        @SuppressLint("MissingPermission")
        @Override
        public void onFindDevice(BluetoothDevice device) {//发现新设备
            if (device.getBondState() == BluetoothDevice.BOND_BONDED) {//已配对
                addDevPaire(BtItemBean.STATE_BONDED, device);
            } else {
                addDevUse(device);
            }
        }
    };

    //蓝牙配对监听
    private IBTBoudListener mBTBoudListener = new IBTBoudListener() {

        /**
         * 设备配对状态改变
         * int BOND_NONE = 10; //配对没有成功
         * int BOND_BONDING = 11; //配对中
         * int BOND_BONDED = 12; //配对成功
         */
        @SuppressLint("MissingPermission")
        @Override
        public void onBondStateChange(BluetoothDevice dev) {
            if (dev.getBondState() == BluetoothDevice.BOND_BONDED) {//已配对
                paireDevStateChange(BtItemBean.STATE_BONDED, dev);
                mBluetoothHelper.connect(dev);
            } else if (dev.getBondState() == BluetoothDevice.BOND_BONDING) {//配对中
                useDevStateChange(BtItemBean.STATE_BONDING, dev);
            } else {//未配对
                BtItemBean btUseItem = findItemByList(mUseAdapter.getData(), dev);
                if (btUseItem != null && btUseItem.getState() == BtItemBean.STATE_BONDING) {
//                    Toast.makeText(getActivity(),"请确认配对设备已打开且在通信范围内",Toast.LENGTH_SHORT).show();
                    ToastUtils.getInstance().show(getActivity(), getString(R.string.blu_confirm_communication_range));
                }
                useDevStateChange(BtItemBean.STATE_BOND_NONE, dev);
            }
        }
    };

    //蓝牙配对监听
    private IBTConnectListener mBTConnectListener = new IBTConnectListener() {
        @Override
        public void onConnecting(BluetoothDevice bluetoothDevice) {//连接中
            paireDevStateChange(BtItemBean.STATE_CONNECTING, bluetoothDevice);
        }

        @Override
        public void onConnected(BluetoothDevice bluetoothDevice) {//连接成功
            paireDevStateChange(BtItemBean.STATE_CONNECTED, bluetoothDevice);

        }

        @Override
        public void onDisConnecting(BluetoothDevice bluetoothDevice) {//断开中
            paireDevStateChange(BtItemBean.STATE_DISCONNECTING, bluetoothDevice);
        }

        @Override
        public void onDisConnect(BluetoothDevice bluetoothDevice) {//断开
            paireDevStateChange(BtItemBean.STATE_DISCONNECTED, bluetoothDevice);
        }

        @Override
        public void onConnectedDevice(List<BluetoothDevice> devices) {//已连接设备
            if (devices == null || devices.size() < 1) {
                return;
            }
            for (BluetoothDevice dev : devices) {
                BtItemBean btUseItem = findItemByList(mPairedAdapter.getData(), dev);
                if (btUseItem != null) {
                    btUseItem.setBluetoothDevice(dev);
                    if (mBluetoothHelper.isConnected(dev)) {
                        btUseItem.setState(BtItemBean.STATE_CONNECTED);
                    } else if (btUseItem.getState() != BtItemBean.STATE_CONNECTED) {
                        btUseItem.setState(BtItemBean.STATE_DISCONNECTED);
                    }
                } else {
                    BtItemBean bluetoothItem = createBluetoothItem(dev);
                    if (mBluetoothHelper.isConnected(dev)) {
                        bluetoothItem.setState(BtItemBean.STATE_CONNECTED);
                    } else {
                        btUseItem.setState(BtItemBean.STATE_DISCONNECTED);
                    }
                    mPairedAdapter.add(0, bluetoothItem);
                }
            }
            mPairedAdapter.notifyDataSetChanged();
        }
    };


    @SuppressLint("MissingPermission")
    @Override
    public void onItemClickListener(BtItemBean btItemBean) {
        final BluetoothDevice bluetoothDevice = btItemBean.getBluetoothDevice();
        switch (btItemBean.getState()) {
            case BtItemBean.STATE_UNCONNECT://未连接
            case BtItemBean.STATE_BOND_NONE://未配对
                mBluetoothHelper.createBond(bluetoothDevice);
                break;
            case BtItemBean.STATE_BONDING://配对中
                break;
            case BtItemBean.STATE_BONDED://已配对

//                TNJCustomDialogUtils.getInstance().initView(getActivity(), getString(R.string.blu_paired_device), TextUtils.isEmpty(bluetoothDevice.getName()) ? bluetoothDevice.getAddress() : bluetoothDevice.getName(), getString(R.string.blu_disconnect), getString(R.string.app_cancel), null, new TNJCustomDialogUtils.OnDialogClickListener() {
//                    @Override
//                    public void onRightClick() {
//                        mBluetoothHelper.removeBond(bluetoothDevice);
//                    }
//
//                    @Override
//                    public void onMiddleClick() {
//                    }
//
//                    @Override
//                    public void onLeftClick() {
//
//                    }
//                });

                simpleDialog = new AlertDialog.Builder(getActivity())
                        .setTitle(getString(R.string.blu_paired_device))
                        .setMessage(TextUtils.isEmpty(bluetoothDevice.getName()) ? bluetoothDevice.getAddress() : bluetoothDevice.getName())
                        .setNegativeButton(getString(R.string.app_cancel), null)
                        .setNeutralButton(getString(R.string.blu_disconnect), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                simpleDialog = null;
                                mBluetoothHelper.removeBond(bluetoothDevice);
                            }
                        }).show();
                break;
            case BtItemBean.STATE_CONNECTING://连接中
                break;
            case BtItemBean.STATE_CONNECTED://已连接
//                TNJCustomDialogUtils.getInstance().initView(getActivity(), getString(R.string.app_connected), TextUtils.isEmpty(bluetoothDevice.getName()) ? bluetoothDevice.getAddress() : bluetoothDevice.getName(), getString(R.string.blu_disconnect), getString(R.string.app_cancel), null, new TNJCustomDialogUtils.OnDialogClickListener() {
//                    @Override
//                    public void onRightClick() {
//                        mBluetoothHelper.disconnect(bluetoothDevice);
//                    }
//
//                    @Override
//                    public void onMiddleClick() {
//                    }
//
//                    @Override
//                    public void onLeftClick() {
//
//                    }
//                });

                simpleDialog = new AlertDialog.Builder(getActivity())
                        .setTitle(getString(R.string.app_connected))
                        .setMessage(TextUtils.isEmpty(bluetoothDevice.getName())?bluetoothDevice.getAddress():bluetoothDevice.getName())
                        .setNegativeButton(getString(R.string.app_cancel),null)
                        .setNeutralButton(getString(R.string.blu_disconnect), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                simpleDialog = null;
                                mBluetoothHelper.disconnect(bluetoothDevice);
                            }
                        }).show();
                break;
            case BtItemBean.STATE_DISCONNECTING://断开中
                break;
            case BtItemBean.STATE_DISCONNECTED://已断开(但还保存)

//                TNJCustomDialogUtils.getInstance().initView(getActivity(), getString(R.string.blu_saved), TextUtils.isEmpty(bluetoothDevice.getName()) ? bluetoothDevice.getAddress() : bluetoothDevice.getName(), getString(R.string.app_connect), getString(R.string.app_cancel), getString(R.string.text_detele), new TNJCustomDialogUtils.OnDialogClickListener() {
//                    @Override
//                    public void onRightClick() {
//                        mBluetoothHelper.connect(bluetoothDevice);
//                    }
//
//                    @Override
//                    public void onMiddleClick() {
//                        mBluetoothHelper.removeBond(bluetoothDevice);
//                    }
//
//                    @Override
//                    public void onLeftClick() {
//
//                    }
//                });

                simpleDialog = new AlertDialog.Builder(getActivity())
                        .setTitle(getString(R.string.blu_saved))
                        .setMessage(TextUtils.isEmpty(bluetoothDevice.getName())?bluetoothDevice.getAddress():bluetoothDevice.getName())
                        .setNegativeButton(getString(R.string.app_cancel),null)
                        .setNeutralButton(getString(R.string.text_detele), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                simpleDialog = null;
                                mBluetoothHelper.removeBond(bluetoothDevice);
                            }
                        })
                        .setPositiveButton(getString(R.string.app_connect), new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                simpleDialog = null;
                                mBluetoothHelper.connect(bluetoothDevice);
                            }
                        }).show();
                break;
        }
    }

    /**
     * 获取已配对设备
     */
    private void getBondedDevices() {//以配对设备
        Set<BluetoothDevice> bluetoothDeviceSet = mBluetoothHelper.getBondedDevices();
        if (bluetoothDeviceSet != null && bluetoothDeviceSet.size() > 0) {
            for (BluetoothDevice device : bluetoothDeviceSet) {
                addDevPaire(BtItemBean.STATE_BONDED, device);
            }
        }
    }

    /**
     * 向已配对列表中添加设备
     *
     * @param state
     * @param dev
     */
    private void addDevPaire(int state, BluetoothDevice dev) {
        BtItemBean btUseItem = findItemByList(mPairedAdapter.getData(), dev);
        if (btUseItem != null) {
            btUseItem.setBluetoothDevice(dev);
        } else {
            BtItemBean bluetoothItem = createBluetoothItem(dev);
            bluetoothItem.setState(state);
            mPairedAdapter.add(0, bluetoothItem);
        }
        mPairedAdapter.notifyDataSetChanged();
    }

    /**
     * 从集合 datas 中找 dev 对应的项
     *
     * @param datas
     * @param dev
     */
    private BtItemBean findItemByList(List<BtItemBean> datas, BluetoothDevice dev) {
        if (datas == null || datas.size() < 1) {
            return null;
        }
        for (BtItemBean btItemBean : datas) {
            if (!TextUtils.isEmpty(dev.getAddress()) && dev.getAddress().equals(btItemBean.getBluetoothDevice().getAddress())) {
                return btItemBean;
            }
        }
        return null;
    }

    private BtItemBean createBluetoothItem(BluetoothDevice device) {
        BtItemBean btItemBean = new BtItemBean();
        btItemBean.setBluetoothDevice(device);
        return btItemBean;
    }

    /**
     * 向可用列表中添加设备
     *
     * @param dev
     */
    @SuppressLint("MissingPermission")
    private void addDevUse(BluetoothDevice dev) {
        BtItemBean btUseItem = findItemByList(mUseAdapter.getData(), dev);
        if (btUseItem != null) {
            btUseItem.setBluetoothDevice(dev);
        } else {
            BtItemBean bluetoothItem = createBluetoothItem(dev);
            if (dev.getBondState() == BluetoothDevice.BOND_BONDED) {
                bluetoothItem.setState(BtItemBean.STATE_BONDED);
            } else if (dev.getBondState() == BluetoothDevice.BOND_BONDING) {
                bluetoothItem.setState(BtItemBean.STATE_BONDING);
            }
            mUseAdapter.add(0, bluetoothItem);
        }
        mUseAdapter.notifyDataSetChanged();
    }

    /**
     * 可用设备列表发生改变
     *
     * @param state
     * @param dev
     */
    private void paireDevStateChange(int state, BluetoothDevice dev) {
        BtItemBean btUseItem = findItemByList(mUseAdapter.getData(), dev);
        BtItemBean btPaireItem = findItemByList(mPairedAdapter.getData(), dev);
        if (btUseItem != null) {
            btUseItem.setState(state);
            btUseItem.setBluetoothDevice(dev);
            mUseAdapter.remove(btUseItem);
            mUseAdapter.notifyDataSetChanged();
            if (btPaireItem != null) {
                mPairedAdapter.remove(btPaireItem);
            }
            mPairedAdapter.add(0, btUseItem);
        } else if (btPaireItem != null) {
            btPaireItem.setState(state);
            btPaireItem.setBluetoothDevice(dev);
        } else {
            BtItemBean bluetoothItem = createBluetoothItem(dev);
            bluetoothItem.setState(state);
            mPairedAdapter.add(0, bluetoothItem);
        }
        mPairedAdapter.notifyDataSetChanged();
    }

    /**
     * 可用设备列表发生改变
     *
     * @param state
     * @param dev
     */
    private void useDevStateChange(int state, BluetoothDevice dev) {
        BtItemBean btUseItem = findItemByList(mUseAdapter.getData(), dev);
        BtItemBean btPaireItem = findItemByList(mPairedAdapter.getData(), dev);
        if (btPaireItem != null) {
            btPaireItem.setState(state);
            btPaireItem.setBluetoothDevice(dev);
            mPairedAdapter.remove(btPaireItem);
            mPairedAdapter.notifyDataSetChanged();
            if (btUseItem != null) {
                mUseAdapter.remove(btUseItem);
            }
            mUseAdapter.add(0, btPaireItem);
        } else if (btUseItem != null) {
            btUseItem.setState(state);
            btUseItem.setBluetoothDevice(dev);
        } else {
            BtItemBean bluetoothItem = createBluetoothItem(dev);
            bluetoothItem.setState(state);
            mUseAdapter.add(0, bluetoothItem);
        }
        mUseAdapter.notifyDataSetChanged();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBluetoothHelper.setBTStateListener(null);//设置打开关闭状态监听
        mBluetoothHelper.setBTScanListener(null);//设置扫描监听
        mBluetoothHelper.setBTBoudListener(null);//设置配对监听
        mBluetoothHelper.setBTConnectListener(null);//设置连接监听
        mBluetoothHelper.destroy();
    }
}
